#include "executor.h"
#include "comm.h"

int main(void)
{
    char buf[BUF_LENGTH] = {0};
    int size = sizeof(buf);
    bool sigexit = true;

    int msgid = getMsgQueue();

    
    while(sigexit)
    {
        recvMsgQueue(msgid, buf);
        sigexit = executeShell(buf, size);
        writeFIFOPipe(buf, size);
        memset(buf, 0, sizeof(buf));
    }

    return 0;
}

bool executeShell(char buf[], int size)
{
    bool ret = true;
    if(strcmp(buf, "exit") == 0)
    {
        ret = false;
    }
    FILE *file;
    char shell[size];
    strcpy(shell, buf);
    memset(buf, 0, size);
    file = popen(shell, "r");
    if(file != NULL)
    {
        while(fgets(shell, size, file) != NULL)
        {
            strcat(buf, shell);
            if(strlen(buf) > size)
                break;
        }
    }
    else
    {
        perror("popen");
        return false;
    }
    pclose(file);
    return true;
}

